import requests
import json
import os
import csv
import re
import sys
import chardet
import requests
import traceback
from PIL import Image
from io import BytesIO
from lxml import etree
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

headers = {
    "cookie": "",
    "user-agent" : "",
    "referer": "https://leetcode-cn.com/problems/",
    "content-type" : "application/json",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "zh,zh-CN;q=0.9",
    "x-csrftoken": ''
}
query= "query questionNote($titleSlug: String!) {↵  question(titleSlug: $titleSlug) {↵    questionId↵    note↵    __typename↵  }↵}↵"
jsonData = {
        'query': '''query getQuestionDetail($titleSlug: String!) {
            question(titleSlug: $titleSlug) {
                questionId
                translatedTitle
                translatedContent
                difficulty
                stats
                topicTags {
                        translatedName
                }
            }
        }''',
"variables": { "titleSlug": "" }
}

rootUrl = 'https://leetcode-cn.com'
rootBag = './test/'
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
# driver = webdriver.Chrome()
driver.maximize_window()

# if not os.path.exists(rootBag):
#     os.makedirs(rootBag)

driver.get('https://leetcode-cn.com/problemset/all/')
sleep(1)
html = etree.HTML(driver.page_source)
problemUrl = html.xpath('//div[@id="current-topic-tags"]/a/@href')
problemNum = html.xpath('//div[@id="current-topic-tags"]/a/span[last()]/text()')
# 题目种类
sortNum = len(problemNum)
for i in range(sortNum):
    sss = re.search( r'(?<=tag/).*?/',str(problemUrl[i]), re.M|re.I)
    sortName = rootBag+sss.group().rstrip("/")
    if not os.path.exists(sortName):
        os.makedirs(sortName)
        gotoSort = rootUrl+str(problemUrl[i])
        driver.get(gotoSort)
        sleep(1)
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        sleep(1)
        # driver.find_element_by_class_name('ant-pagination-options-size-changer').click()
        element = driver.find_element_by_class_name('ant-pagination-options-size-changer')
        driver.execute_script("arguments[0].click();", element)
        sleep(1)
        driver.find_elements_by_class_name('ant-select-dropdown-menu-item')[3].click()
        sleep(3)
        sortHtml = etree.HTML(driver.page_source)
        toproblemUrl = sortHtml.xpath('//tbody[@class="ant-table-tbody"]/tr/@data-row-key')
        # 每一个题目的链接
        everyList = len(toproblemUrl)
        for n in range(everyList):
            try:
                url = str(toproblemUrl[n])
                jsonData['variables']['titleSlug']=url
                urls = 'https://leetcode-cn.com/graphql/'
                data = requests.post(urls,data=json.dumps(jsonData),headers=headers).json()
                problemId = data['data']['question']['questionId']
                problemEZ = data['data']['question']['difficulty']
                problemName = data['data']['question']['translatedTitle']
                main = data['data']['question']['translatedContent']
                if(main == None):
                    print("这是会员题")
                else:
                    c = json.dumps(data['data']['question'])
                    idtxt = json.dumps({"problemID":data['data']['question']['questionId']})
                    print("正在下载"+problemName)
                    with open('./test/'+'all.json','a') as f:
                        f.write(c)
                    with open('./test/'+'id.json','a') as files:
                        files.write(idtxt)
            except Exception as err:
            # #跳过错误代码
                traceback.print_exc()
    else:
        print('NIU')
        pass
